﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Edit_PurchaseOrder : Form
    {
        public frm_Edit_PurchaseOrder()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            getSupplierList();
            getDeliveryList();
            getShipToList();
            getPaymentList();            
        }

        ////////////////////////////////////////////////
        /// Load PO information to form
        ////////////////////////////////////////////////
        public bool LoadData(string id)
        {
            this.POID = id;
            if (this.POID != "")
            {
                try
                {
                    int i;
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [241_PO] WHERE ID='" + this.POID + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.poNo.Text = dataReader["ID"].ToString();
                        DateTime input_date;
                        DateTime.TryParse(dataReader["InputDate"].ToString(), out input_date);
                        this.inputDate.Text = input_date.ToShortDateString();
                        this.commodityName.Text = dataReader["NameOfCommodity"].ToString();
                        for (i = 0; i < this.supplierID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.supplierID.Properties.Items[i];
                            if (item.Value.Equals(dataReader["SupplierID"].ToString()))
                            {
                                this.supplierID.SelectedIndex = i;
                            }
                        }                        
                        this.commodityName.Text = dataReader["NameOfCommodity"].ToString();
                        for (i = 0; i < this.deliveryID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.deliveryID.Properties.Items[i];
                            if (item.Value.Equals(dataReader["DeliveryTermID"].ToString()))
                            {
                                this.deliveryID.SelectedIndex = i;
                            }
                        }
                        for (i = 0; i < this.shiptoID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.shiptoID.Properties.Items[i];
                            if (item.Value.Equals(dataReader["ShipToCode"].ToString()))
                            {
                                this.shiptoID.SelectedIndex = i;
                            }
                        }
                        for (i = 0; i < this.paymentID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.paymentID.Properties.Items[i];
                            if (item.Value.Equals(dataReader["PaymentID"].ToString()))
                            {
                                this.paymentID.SelectedIndex = i;
                            }
                        }
                        this.remarkText.Text = dataReader["Remark"].ToString();
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    return true;
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show("Cannot get Data for PO ID: " + POID);
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        ////////////////////////////////////////////////
        /// Load Supplier list to form
        ////////////////////////////////////////////////
        private void getSupplierList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT SupplierID, SupplierName FROM \"222_Supplier\" ORDER BY SupplierName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["SupplierName"].ToString();
                    item.Value = dataReader["SupplierID"].ToString();
                    this.supplierID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();                
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Supplier list");                
            }
        }

        ////////////////////////////////////////////////
        /// Load Delivery list to form
        ////////////////////////////////////////////////
        private void getDeliveryList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DeliveryTermID, DeliveryTermName FROM \"222_DeliveryTerm\" ORDER BY DeliveryTermName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["DeliveryTermName"].ToString();
                    item.Value = dataReader["DeliveryTermID"].ToString();
                    this.deliveryID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Delivery list");
            }
        }

        ////////////////////////////////////////////////
        /// Load Ship To list to form
        ////////////////////////////////////////////////
        private void getShipToList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT ShipToCode, ShipToName FROM \"222_ShipTo\" ORDER BY ShipToName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["ShipToName"].ToString();
                    item.Value = dataReader["ShipToCode"].ToString();
                    this.shiptoID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Ship To list");
            }
        }

        ////////////////////////////////////////////////
        /// Load Ship To list to form
        ////////////////////////////////////////////////
        private void getPaymentList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT PaymentID, PaymentName FROM \"222_Payment\" ORDER BY PaymentName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["PaymentName"].ToString();
                    item.Value = dataReader["PaymentID"].ToString();
                    this.paymentID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Payment list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }    
}
